import Vue from 'vue';
import Router from 'vue-router';
import iView from 'iview';
import routes from './routers';
import { getCookie } from '@/util';
Vue.use(Router);

const router = new Router({
    mode: 'hash',
    scrollBehavior: () => ({ y: 0 }),
    routes,
});

/* 前置守卫 */
router.beforeEach((to, from, next) => {
    /* 加载进度条 */
    iView.LoadingBar.start();
    if (!sessionStorage.getItem('user') || !getCookie('tkcloud_loginuserkey')) {
        if (to.path === '/login') {
            next();
        } else if (to.path === '/webview') {
            next();
        } else if (to.path == '/info') {
            next();
        } else if (to.path == '/netNurse') {
            next();
        }
        else {
            next({
                path: '/login',
            });
        }
    } else {
        next();
    }
});
/* 后置守卫 */
router.afterEach(() => {
    /* 进度条结束 */
    iView.LoadingBar.finish();
});

export default router;
